package com.crrcdt.vue_project.controller;

import cn.hutool.core.lang.tree.Tree;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.crrcdt.vue_project.entity.Category;
import com.crrcdt.vue_project.entity.Permission;
import com.crrcdt.vue_project.entity.Role;
import com.crrcdt.vue_project.entity.vo.CateVo;
import com.crrcdt.vue_project.result.Result;
import com.crrcdt.vue_project.result.ResultFactory;
import com.crrcdt.vue_project.service.CategoryService;
import com.crrcdt.vue_project.util.PageUtil;
import com.crrcdt.vue_project.util.TreeUtils;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author lj on 2020/10/22.
 * @version 1.0
 */
@RestController
@RequestMapping(value = "/api/private/v1")
@Slf4j
public class CateController {
    @Autowired
    private CategoryService categoryService;
    @GetMapping("/categories")
    public Result getCategories(@RequestParam(name = "type",required = false)String type,
                                @RequestParam(name = "pagenum",required = false) Integer pagenum,
                                @RequestParam(name = "pagesize",required = false)Integer pagesize) {
        HashMap<Object, Object> data = new HashMap<>();
        // 得到idAndStrMap
        final HashMap<Integer, CateVo> idAndStrMap = new HashMap<>();
        final List<Category> categories = categoryService.list(null);
        categories.stream().forEach(category -> {
            final CateVo cateVo = new CateVo();
            cateVo.setCatLevel(category.getCatLevel());
            cateVo.setCatDeleted(category.getCatDeleted());
            idAndStrMap.put(category.getCatId(),cateVo);
        });
        //获取所有分类
        if(type==null || StringUtils.equals(type,"3")){
            if(pagenum == null && pagesize == null){
                final List<Tree<String>> cateTrees = TreeUtils.getCateTrees(categories, idAndStrMap);
                return ResultFactory.buildSuccessResult(cateTrees, "获取成功");
            }else {
                final List<Tree<String>> cateTrees = TreeUtils.getCateTrees(categories, idAndStrMap);
                data.put("total", cateTrees.size());
                data.put("pagenum", pagenum);
                final PageUtil<Tree<String>> treePageUtil = new PageUtil<>();
                treePageUtil.pageUtil(pagenum -1 ,pagesize,cateTrees);
                data.put("cateTreeList", treePageUtil.getContent());
                return ResultFactory.buildSuccessResult(data, "获取成功");
            }
        }
        if(StringUtils.equals(type,"2")){
            final List<Category> categoryList = categoryService.list(new QueryWrapper<Category>().ne("cat_level", 2));
            final List<Tree<String>> cateTrees = TreeUtils.getCateTrees(categoryList, idAndStrMap);
            return ResultFactory.buildSuccessResult(cateTrees, "获取成功");
        }
        return ResultFactory.buildSuccessResult(null,"获取成功");
    }
    // 添加分类
    @PostMapping("/categories")
    public Result addCategories(@RequestBody Category category) {
        final boolean res = categoryService.save(category);
        if(res){
          return ResultFactory.buildSuccessResult(category,"创建成功！");
        }
          return ResultFactory.buildFailResult("创建失败！");
    }
    // 根据 id 查询分类
    @GetMapping("/categories/{id}")
    public Result getCategoriesById(@PathVariable("id") Integer id) {
        final Category category = categoryService.getById(id);
        if(category!=null){
            return ResultFactory.buildSuccessResult(category,"获取成功！");
        }
        return ResultFactory.buildFailResult("获取失败！");
    }
    //编辑提交分类
    @PutMapping("/categories/{id}")
    public Result updateCategoriesById(@PathVariable("id") Integer id,
                                     @RequestBody Category category) {
        category.setCatId(id);
        final boolean res = categoryService.updateById(category);
        if(res){
            final Category categoryNew = categoryService.getById(id);
            return ResultFactory.buildSuccessResult(categoryNew,"更新成功！");
        }
        return ResultFactory.buildFailResult("更新失败！");
    }
    // 删除分类
    @DeleteMapping("/categories/{id}")
    public Result updateCategoriesById(@PathVariable("id") Integer id) {
        final boolean res = categoryService.removeById(id);
        if(res){
            return ResultFactory.buildSuccessResult(null,"删除成功！");
        }
        return ResultFactory.buildFailResult("删除失败！");
    }
}

